package collection;

import java.util.ArrayList;
import java.util.List;

public class SubListDemo {
    public static void main(String[] args) {
        /*
        List提供了获取子集的操作;
        List subList(int start,int end)
         */
        List<Integer> list=new ArrayList<>();
        for(int i=0;i<10;i++){
            list.add(i*10);
        }
        System.out.println(list);//[0, 10, 20, 30, 40, 50, 60, 70, 80, 90]

        List<Integer> list1=list.subList(3,8);
        System.out.println(list1);//[30, 40, 50, 60, 70]

        //将子集扩大10倍
        for(int i=0;i<list1.size();i++){
            list1.set(i,list1.get(i)*10);
        }
        System.out.println(list1);

        //对子集改变后原集合也会跟着改变
        System.out.println(list);//[0, 10, 20, 300, 400, 500, 600, 700, 80, 90]
        list.set(4,1000);
        //对原集合改变后子集也会跟着改变
        System.out.println(list);//[0, 10, 20, 300, 1000, 500, 600, 700, 80, 90]
        System.out.println(list1);//[300, 1000, 500, 600, 700]

        //原集合长度修改后,子集将不能进行任何操作,否则发生异常
        list.remove(0);
        System.out.println(list);//[10, 20, 300, 1000, 500, 600, 700, 80, 90]
        System.out.println(list1);//输出异常

    }
}
